package com.laikan.reader.bookfriend.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.laikan.library.components.activitys.BasicActivity;
import com.laikan.library.managers.ToastMgr;
import com.laikan.library.utils.SPUtil;
import com.laikan.reader.R;
import com.laikan.reader.bookreader.activity.BookDesActivity;
import com.laikan.reader.core.utils.LoadingDialog;
import com.laikan.reader.core.utils.MD5Util;
import com.laikan.reader.core.utils.PublicUtil;
import com.umeng.analytics.MobclickAgent;

/**
 * webview  页面集合
 */
@SuppressLint ("SetJavaScriptEnabled")
public class WebViewActivity2 extends BasicActivity implements OnClickListener {

    private ImageView back_tv;
    private String url;
    private TextView title_name;
    private WebView webview;
    private LoadingDialog loadingDialog;
    private String title;
    private RelativeLayout back_layout;
    Drawable drawable_back_tv;

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void assignViews() {
        setContentView(R.layout.lk_webview);
        title_name = (TextView)findViewById(R.id.tv_title);
        back_tv = (ImageView)findViewById(R.id.iv_back);
        back_layout =(RelativeLayout)findViewById(R.id.back_layout);
        webview = (WebView)findViewById(R.id.webview);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        webview.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (100 == newProgress) {
                    if (null != loadingDialog && loadingDialog.isShowing()) {
                        loadingDialog.dismiss();
                    }
                }
            }
        });
    }

    @Override
    protected void initData() {
        drawable_back_tv = new BitmapDrawable(PublicUtil.getAlphaBitmap(PublicUtil.readBitMap(mContext,R.drawable.lk_back_small), Color.parseColor("#66ccdd")));
        back_tv.setImageDrawable(drawable_back_tv);
        url = getIntent().getStringExtra("mHtmlUrl");
        title = getIntent().getStringExtra("title");
        title_name.setText(title);
        loadingDialog = new LoadingDialog(mContext);
        long systime = System.currentTimeMillis();
        String userId = SPUtil.getString("user_id", "0");
        String sign = MD5Util.getMD5Str(userId + systime + "ssx.yJBWrqw)nb,xV,I8");
        webview.setWebViewClient(new WebViewClient());
        //		webview.loadUrl(mHtmlUrl + "?userId=" + userId + "&time=" + systime
        //				+ "&sign=" + MD5Util.getMD5Str(sign));
        //		webview.loadUrl(mHtmlUrl);
        webview.postUrl(url, ("sd=" + SPUtil.getString("token", "")).getBytes());
        // webview.loadUrl("http://www.sina.com");
        // 不设置这个监听，webview就不会启用，会调用浏览器
        if (null != loadingDialog && !loadingDialog.isShowing()) {
            loadingDialog.show();
        }
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {

                super.onPageStarted(view, url, favicon);
                if (url.contains("/book/")) {
                    String str = url.replace("http://m.laikan.com/book/", "");
                    startActivityForResult(new Intent(mContext, BookDesActivity.class).putExtra("bookId", str), 007);

                    return;
                }

            }

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                ToastMgr.showShortToast("" + errorCode);
            }

            public void onPageFinished(WebView view, String url) {
                // 判断 返回URL中是否有跳转详情页的标识，如有则跳转
            }

        });

    }

    @Override
    protected void onActivityResult(int arg0, int arg1, Intent arg2) {
        if (arg0 == 007) {
            webview.goBack();
        }
        super.onActivityResult(arg0, arg1, arg2);

    }

    @Override
    protected void setListener() {
        back_layout.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_layout:
                if (webview.canGoBack()) {
                    webview.goBack();

                    return;
                }
                finish();
                break;

            default:
                break;
        }
    }

}
